package com.up.stores.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.up.stores.entity.Admin;
import com.up.stores.entity.Request;
import com.up.stores.entity.User;
import com.up.stores.mapper.AdminMapper;
import com.up.stores.mapper.RequestMapper;
import com.up.stores.mapper.UserMapper;
import com.up.stores.service.IRequestService;
import com.up.stores.service.ex.DeleteException;
import com.up.stores.service.ex.InsertException;
import com.up.stores.service.ex.RequestSubmitException;
import com.up.stores.service.ex.UserNotFoundException;
import com.up.stores.vo.RequestDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Service
public class RequestServiceImpl implements IRequestService {
    @Autowired
    private RequestMapper requestMapper;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private  BaseUserServiceImpl userService;

    @Override
    public void submitReq(Request req) {
        QueryWrapper<User> queryWrapper0 = new QueryWrapper<>();
        queryWrapper0.eq("user_name",req.getNorName());
        User user = userMapper.selectOne(queryWrapper0);
        if (user.getAdmId()!=null) { throw new RequestSubmitException("用户已归属于某实验室"); }

        //查找管理员名，若不存在该管理员则提示错误信息
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_name",req.getAdmName());
        Admin admin = adminMapper.selectOne(queryWrapper);
        if(admin == null){
            throw new UserNotFoundException("提交的管理员账号不存在");
        }
        /*补全数据:申请状态，四个日志字段信息*/
        req.setReqStatus(0);
        req.setCreatedUser(req.getNorName());
        req.setModifiedUser(req.getNorName());
        req.setCreatedTime(new Date());
        req.setModifiedTime(new Date());

        /*执行提交申请的业务功能*/
        Integer rows = requestMapper.insertRequest(req);
        if (rows != 1) {
            throw new InsertException("在用户提交过程中产生了未知的异常");
        }
    }

    @Override
    public void revokeReq(Integer reqId) {
        QueryWrapper<Request> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("req_id", reqId);
        Integer row = requestMapper.delete(queryWrapper);
        if(row != 1){
            throw new DeleteException("在用户删除过程中产生了未知的异常");
        }
    }

    @Override
    public void approvalReq(Integer reqId, Integer reqStatus, String modifiedUser, Date modifiedTime) {
        /*User result = userMapper.findByUid(uid);
        if (result ==null || result.getIsDelete() == 1) {
            throw new UsernameNotFoundException("用户数据不存在");
        }*/
        /*执行提交申请的业务功能*/
        Integer rows = requestMapper.updateStatusByReqId(reqId, reqStatus, modifiedUser, modifiedTime);
        if (rows != 1) {
            throw new InsertException("在管理员审批过程中产生了未知的异常");
        }
    }
    //@Override

//    public Boolean judgeReq(Integer norId) {
//        /*执行判断是否已提交过一次申请业务功能*/
//        Boolean flag;
//        QueryWrapper<Request> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("nor_id",norId);
//        Request request = requestMapper.selectOne(queryWrapper);
//        if(request == null){
//            flag = false;//表示未有管理员
//        }else{
//            flag = true;//表示已有管理员
//        }
//        return flag;
//    }

    @Override
    public List<RequestDTO> findByAdmNameAndReqStatus(String admName, Integer reqStatus) {
        /*执行提交申请的业务功能*/
        QueryWrapper<Request> queryWrapper0 = new QueryWrapper<>();
        queryWrapper0.eq("adm_name", admName).eq("req_status", reqStatus);
        List<Request> list = requestMapper.selectList(queryWrapper0);
        if (list == null) {
            throw new InsertException("在查找过程中产生了未知的异常");
        }
        //循环list，查找出该普通用户的真实姓名和手机号并填入requestDTO中一起返回
        List<RequestDTO> listDTO = new LinkedList<>();
        for(Request req : list){
            //查找
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_name", req.getNorName());
            User user = userMapper.selectOne(queryWrapper);
            //填入DTO
            RequestDTO requestDTO = new RequestDTO();
            BeanUtils.copyProperties(req,requestDTO);
            System.out.println(requestDTO);
            if(user != null){
                requestDTO.setName(user.getRealName());
                requestDTO.setPhone(user.getPhone());
                requestDTO.setNorId(user.getNorId());
                requestDTO.setAvatar(user.getAvatar());
            }
            listDTO.add(requestDTO);
        }
        return listDTO;
    }

    @Override
    public void deleteUser(String norName) {
        /*管理员执行删除某归属其的普通用户的业务功能*/
        //修改User表中记录的信息
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_name", norName)
                     .set("adm_id", null )
                     .set("lib_id", null);
        userMapper.update(null, updateWrapper);
        //删除该申请记录
        QueryWrapper<Request> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("nor_name", norName);
        requestMapper.delete(queryWrapper);
    }
}
